package com.lt.system;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;

import com.lt.common.DebugHelper;

import java.lang.reflect.Method;

public class DeviceHelper
{
    private static DeviceHelper instance;
    private static final String XIAOMI_BRAND = "xiaomi";

    private DeviceHelper()
    {
    }

    public static DeviceHelper getInstance()
    {
        if (instance == null)
        {
            synchronized (DeviceHelper.class)
            {
                instance = new DeviceHelper();
            }
        }
        return instance;
    }

    /**
     * 检测是否为小米设备
     */
    public boolean isXiaomiDevice()
    {
        return XIAOMI_BRAND.equalsIgnoreCase(Build.MANUFACTURER) ||
                XIAOMI_BRAND.equalsIgnoreCase(Build.BRAND);
    }

    /**
     * 检测 MIUI 版本
     */
    public String getMiuiVersion()
    {
        String miuiVersion = "";
        try
        {
            Class<?> systemProperties = Class.forName("android.os.SystemProperties");
            Method getMethod = systemProperties.getMethod("get", String.class, String.class);
            miuiVersion = (String) getMethod.invoke(null, "ro.miui.ui.version.name", "");
        } catch (Exception e)
        {
            DebugHelper.e("Error getting MIUI version:" + e.getMessage());
        }
        return miuiVersion;
    }


    /**
     * 打开应用设置界面
     */
    public void openAppSettings(Context context)
    {
        try
        {
            Intent intent = new Intent(android.provider.Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
            intent.setData(Uri.parse("package:" + context.getPackageName()));
            context.startActivity(intent);
        } catch (Exception e)
        {
            DebugHelper.e("Error opening app settings:" + e.getMessage());
        }
    }
}
